//
//  MainIndexViewController.m
//  Fangmm
//
//  Created by 阮琦 on 16/2/29.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "MainIndexViewController.h"
#import "SearchViewController.h"
#import "HouseDetailViewController.h"
#import "FilterPriceViewController.h"
#import "FilterRegionsViewController.h"
#import "FilterSortByViewController.h"
#import "WebViewController.h"
#import "RecommendViewController.h"
#import "UserHomepageViewController.h"
#import "ChooseCityViewController.h"

#import "HouseListTableViewCell.h"

#import "BannerView.h"
#import "EmptyView.h"
#import "FillRequirementView.h"

#import "HttpServer.h"
#import "House.h"
#import "Team.h"
#import "Seat.h"

#import <SDWebImage/UIImageView+WebCache.h>
#import <SDWebImage/UIButton+WebCache.h>
#import <MobClick.h>
#import "MJRefresh.h"
#import <MobClick.h>
#import "Utility.h"
#import "Constants.h"
#import "ACMacros.h"

#import "AppDrawer.h"

@interface MainIndexViewController () <UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate, UITextFieldDelegate, FilterRegionsViewDelegate, FilterSortByViewDelegate, FilterPriceViewDelegate, BannerScrollViewDelegate, HouseListTableViewCellDelegate, FillRequirementDelegate>

@end

@implementation MainIndexViewController {
    UITableView *mainTableView;
    UIView *searchView;
    BannerView *banner;
    FilterPriceViewController *filterPriceVC;
    FilterRegionsViewController *filterRegionsVC;
    FilterSortByViewController *filterSortByVC;
    SearchViewController *searchVC;
    ChooseCityViewController *cityVC;
    FillRequirementView *fillRequirementView;
    
    NSMutableDictionary *params;
    NSMutableArray *housesArray;
    NSMutableArray *teams;
    
    int page;
    int pageSize;
    
    UIButton *filterRegionButton;
    NSString *filterRegionButtonString;
    UIButton *filterPriceButton;
    NSString *filterPriceButtonString;
    UIButton *filterSortByButton;
    NSString *filterSortByButtonString;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self defineParams];
    [self configureNavigationBar];
    [self configureTableView];
    [self configureFilterViews];
    [self loadHousesData];
    [self loadAds];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
//    if (mainTableView && mainTableView.mj_footer.state != MJRefreshStateNoMoreData) {
//        [self refreshData];
//    }
    [MobClick beginLogPageView:@"主页"];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [MobClick endLogPageView:@"主页"];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)defineParams {
    page = 1;
    pageSize = 10;
    NSString *cityCode = [Utility getObjectForKey:kCityCode];
    if (cityCode == nil || [cityCode isEqual:@""]) {
        cityCode = @"3502";
    }
    NSDictionary *dic = @{@"cityCode":cityCode, @"page":[NSString stringWithFormat:@"%d",page], @"pageSize":[NSString stringWithFormat:@"%d",pageSize]};
    params = [[NSMutableDictionary alloc]initWithDictionary:dic];
    
    filterRegionButtonString = @"区域";
    filterPriceButtonString = @"价格";
    filterSortByButtonString = @"排序";
}

#pragma mark - Configuring the view’s layout behavior
- (void)configureNavigationBar {
    self.title = @"房妈妈";
    self.navigationItem.hidesBackButton = true;
    UIImage *backImage = [[UIImage imageNamed:@"mine-68-50"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithImage:backImage style:UIBarButtonItemStyleDone target:self action:@selector(clickToOpenMenu)];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    UIImage *rightImage = [UIImage imageNamed:@"push"];
    UIBarButtonItem *rightBar = [[UIBarButtonItem alloc]initWithImage:[rightImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(clickToRecommend)];
    self.navigationItem.rightBarButtonItem = rightBar;

}

- (void)configureBanner {
    UIView *tableHeader = [[UIView alloc]init];
    banner = [[BannerView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, 0.47 * Main_Screen_Width)];
    [tableHeader setFrame:banner.frame];
    [tableHeader addSubview:banner];
    [mainTableView setTableHeaderView:tableHeader];
    [banner setBackgroundColor:[UIColor whiteColor]];
    [banner setDelegate:self];
    CGFloat distance = 0.025 * Main_Screen_Width;
    CGFloat searchViewHeight = 0.1 * Main_Screen_Width;
    CGFloat searchViewWidth = banner.frame.size.width - 2 * distance;
    searchView = [[UIView alloc]initWithFrame:CGRectMake(distance, banner.frame.size.height - distance - searchViewHeight, Main_Screen_Width - 2 * distance, searchViewHeight)];
    searchView.backgroundColor = [UIColor whiteColor];
    UIButton *cityButton = [[UIButton alloc]initWithFrame:CGRectMake(7, 0, 43, searchViewHeight)];
    UIImageView *down = [[UIImageView alloc]initWithFrame:CGRectMake(32, 0, 5, searchViewHeight)];
    down.image = [UIImage imageNamed:@"down"];
    [cityButton addSubview:down];
    [down setContentMode:UIViewContentModeScaleAspectFit];
    [cityButton setTitle:@"厦门" forState:UIControlStateNormal];
    [cityButton setTitleColor:RGBCOLOR(71, 62, 58) forState:UIControlStateNormal];
    [cityButton addTarget:self action:@selector(clickToChooseCity) forControlEvents:UIControlEventTouchUpInside];
    [cityButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [cityButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
    [cityButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
    UIView *lightline = [[UIView alloc]initWithFrame:CGRectMake(50, 0, 0.8, searchViewHeight)];
    lightline.backgroundColor = RGBACOLOR(204, 204, 204, 0.5);
    UIImageView *searchImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.028 * searchViewWidth + 50, 0, 0.043 * searchViewWidth, searchViewHeight)];
    searchImageView.image = [UIImage imageNamed:@"search"];
    searchImageView.contentMode = UIViewContentModeScaleAspectFit;
    UITextField *searchTextField = [[UITextField alloc]initWithFrame:CGRectMake(0.1 * searchViewWidth + 50, 0, 0.678 * searchViewWidth - 50, searchViewHeight)];
    [searchTextField setDelegate:self];
    [searchTextField setFont:[UIFont systemFontOfSize:12]];
    [searchTextField setPlaceholder:@"请输入您的目标商圈或小区名"];
    UIButton *searchButton = [[UIButton alloc]initWithFrame:CGRectMake(0.798 * searchViewWidth, 0, 0.202 * searchViewWidth, searchViewHeight)];
    [searchButton setBackgroundColor:RGBACOLOR(255, 118, 108, 1)];
    [searchButton setTitle:@"找合租房" forState:UIControlStateNormal];
    [searchButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [searchButton.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [searchButton addTarget:self action:@selector(clickToSearch) forControlEvents:UIControlEventTouchUpInside];
    [searchView addSubview:searchImageView];
    [searchView addSubview:cityButton];
    [searchView addSubview:lightline];
    [searchView addSubview:searchTextField];
    [searchView addSubview:searchButton];
    [tableHeader addSubview:searchView];
}

- (void)configureTableView {
    mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Height - 20) style:UITableViewStylePlain];
    [mainTableView setDelegate:self];
    [mainTableView setDataSource:self];
    [mainTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [mainTableView registerNib:[UINib nibWithNibName:@"HouseListTableViewCell" bundle:nil] forCellReuseIdentifier:@"HouseListTableViewCell"];
    [mainTableView setBackgroundColor:RGBACOLOR(240, 240, 240, 1)];
    [self.view addSubview:mainTableView];
    mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(pullToRefreshData)];
    mainTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [self configureBanner];
}

- (void)configureFilterViews {
    filterPriceVC = [[FilterPriceViewController alloc]initWithNibName:@"FilterPriceViewController" bundle:nil];
    [filterPriceVC setDelegate:self];
    filterRegionsVC = [[FilterRegionsViewController alloc]initWithNibName:@"FilterRegionsViewController" bundle:nil];
    [filterRegionsVC setDelegate:self];
    filterSortByVC = [[FilterSortByViewController alloc]initWithNibName:@"FilterSortByViewController" bundle:nil];
    NSArray *order = @[@"默认", @"价格从低到高", @"厅室从高到低", @"室长从多到少"];
    filterSortByVC.sourceDataArray = order;
    [filterSortByVC setDelegate:self];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (housesArray.count % 10 != 0 || (housesArray.count == 0)) {
        return housesArray.count + 1;
    } else {
        return housesArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ((housesArray.count == indexPath.row && mainTableView.mj_footer.state == MJRefreshStateNoMoreData )|| housesArray.count == 0) {
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        fillRequirementView = [[FillRequirementView alloc] init];
        fillRequirementView = [[[NSBundle mainBundle]loadNibNamed:@"FillRequirementView" owner:self options:nil] firstObject];
        fillRequirementView.delegate = self;
        [cell addSubview:fillRequirementView];
        return cell;
    } else {
        NSInteger row = indexPath.row;
        House *house = [[House alloc]initWithDic:[housesArray[row] objectForKey:@"house"]];
        teams = [housesArray[row] objectForKey:@"teams"];
        HouseListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HouseListTableViewCell"];
        [cell setDelegate:self];
        cell.tag = row;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.houseImageView sd_setImageWithURL:[NSURL URLWithString:house.cover] placeholderImage:[UIImage new]];
        [cell.priceLabel setText:[NSString stringWithFormat:@"¥%d 间",house.singleRentFloor]];
        [cell.houseNameLabel setText:house.community];
        [cell.houseRemarkLabel setText:[NSString stringWithFormat:@"%@ %d室%d厅%d卫 - %dm² | 入住时间: %@",house.region, house.roomNum, house.hallNum, house.toiletNum, house.area, house.checkinTime]];
        if (house.check) {
            [cell.houseCheckLabel setHidden:false];
            [cell.sourceLeading setConstant:30];
        } else {
            [cell.houseCheckLabel setHidden:true];
            [cell.sourceLeading setConstant:0];
        }
        [cell.houseSourceLabel setText:house.from];
        NSArray *buttonArray = @[cell.headerButton_1, cell.headerButton_2, cell.headerButton_3, cell.headerButton_4, cell.headerButton_5];
        for (UIButton *button in buttonArray) {
            button.hidden = true;
        }
        
        for (int i = 0; i < teams.count; i++) {
            if (i < buttonArray.count) {
                UIButton *button = buttonArray[i];
                button.hidden = false;
                Team *team = [[Team alloc]initWithDic:teams[i]];
                [button sd_setBackgroundImageWithURL:[NSURL URLWithString:team.leaderAvatar] forState:UIControlStateNormal];
            }
        }
        if (teams.count == 0) {
            [cell.noteLabel setTextAlignment:NSTextAlignmentLeft];
            [cell.noteLabel setText:@"暂无室友小组,快来招募你的室友吧!"];
        } else {
            [cell.noteLabel setTextAlignment:NSTextAlignmentRight];
            [cell.noteLabel setText:[NSString stringWithFormat:@"%d个室长在招募",teams.count]];
        }
        return cell;
    }
}

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (housesArray.count == indexPath.row || housesArray.count == 0) {
        return 630;
    } else {
        NSInteger row = indexPath.row;
        House *house = [[House alloc]initWithDic:[housesArray[row] objectForKey:@"house"]];
        NSNumber *code = [[NSNumber alloc]initWithInteger:[[house.limits[0] objectForKey:@"code"] integerValue]];
        switch ([code integerValue]) {
            case 1:
                return Main_Screen_Width * 0.6 + 123;
                break;
            default:
                return Main_Screen_Width * 0.6 + 174;
                break;
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 45;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *sectionHeader = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, 45)];
    [sectionHeader setBackgroundColor:[UIColor whiteColor]];
    filterRegionButton = [[UIButton alloc]initWithFrame:CGRectMake(10, 0, Main_Screen_Width / 3, 45)];
    [filterRegionButton addTarget:self action:@selector(clickToFilterRegions) forControlEvents:UIControlEventTouchUpInside];
    [filterRegionButton setImage:[UIImage imageNamed:@"area-40-40"] forState:UIControlStateNormal];
    [filterRegionButton setTitle:filterRegionButtonString forState:UIControlStateNormal];
    
    
    filterPriceButton = [[UIButton alloc]initWithFrame:CGRectMake(Main_Screen_Width / 3 + 10, 0, Main_Screen_Width / 3, 45)];
    [filterPriceButton addTarget:self action:@selector(clickToFilterPrice) forControlEvents:UIControlEventTouchUpInside];
    [filterPriceButton setImage:[UIImage imageNamed:@"screen-out-36-40"] forState:UIControlStateNormal];
    [filterPriceButton setTitle:filterPriceButtonString forState:UIControlStateNormal];
    
    filterSortByButton = [[UIButton alloc]initWithFrame:CGRectMake(2 * Main_Screen_Width / 3 + 10, 0, Main_Screen_Width / 3, 45)];
    [filterSortByButton addTarget:self action:@selector(clickToFilterSortBy) forControlEvents:UIControlEventTouchUpInside];
    [filterSortByButton setImage:[UIImage imageNamed:@"sequencer-28-40"] forState:UIControlStateNormal];
    [filterSortByButton setTitle:filterSortByButtonString forState:UIControlStateNormal];
    
    NSArray *buttonArray = @[filterRegionButton, filterPriceButton, filterSortByButton];
    for (UIButton *button in buttonArray) {
        [button setTitleColor:RGBACOLOR(71, 62, 58, 1) forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont systemFontOfSize:12]];
        [button setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 30)];
        [button setContentMode:UIViewContentModeCenter];
        [sectionHeader addSubview:button];
    }

    return sectionHeader;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (housesArray.count == indexPath.row && mainTableView.mj_footer.state == MJRefreshStateNoMoreData) {
    } else {
        NSInteger row = indexPath.row;
        HouseDetailViewController *detailVC = [[HouseDetailViewController alloc]init];
        if (housesArray[row] == nil) {
        } else {
            House *house = [[House alloc]initWithDic:[housesArray[row] objectForKey:@"house"]];
            detailVC.houseId = house.houseId;
            [self.navigationController pushViewController:detailVC animated:true];
        }
    }
}

#pragma mark - Triggering Methods
- (void)clickToOpenMenu {
    if ([AppDrawer sharedDrawer].isInAction) {
        NSLog(@"...........");
    } else {
        if ([AppDrawer sharedDrawer].isOpen) {
            [[AppDrawer sharedDrawer] close];
        } else {
            [[AppDrawer sharedDrawer] open];
        }
    }
}

- (void)clickToRecommend {
    RecommendViewController *recommendVC = [[RecommendViewController alloc]init];
    [self.navigationController pushViewController:recommendVC animated:true];
}

- (void)clickToFilterRegions {
    [self presentViewController:filterRegionsVC animated:true completion:^{
        
    }];
}

- (void)clickToFilterPrice {
    [self presentViewController:filterPriceVC animated:true completion:^{
        
    }];
}

- (void)clickToFilterSortBy {
    [self presentViewController:filterSortByVC animated:true completion:^{
        
    }];
}

- (void)clickToSearch {
    if (!searchVC) {
        searchVC = [[SearchViewController alloc]initWithNibName:@"SearchViewController" bundle:nil];
    }
    [self.navigationController pushViewController:searchVC animated:true];
    NSLog(@"");
}

#pragma mark - Network methods
- (void)loadHousesData {
    [HttpServer getHousesWithParams:params WithSuccessBlock:^(NSDictionary *housesData) {
        if (!housesArray) {
            housesArray = [[NSMutableArray alloc]init];
        }
        [housesArray addObjectsFromArray:[housesData objectForKey:@"houses"]];
        [mainTableView reloadData];
        [mainTableView.mj_header endRefreshing];
        if (housesArray.count == 0) {
        } else {
            page += 1;
        }
        if (housesArray.count % 10 != 0 || housesArray.count == 0) {
            [mainTableView.mj_footer endRefreshingWithNoMoreData];
            [self refreshToRequirement];
        } else {
            [mainTableView.mj_footer endRefreshing];
        }
    } withFailBlock:^(id failData) {
        NSLog(@"%@", failData);
    }];
}

- (void)loadAds {
    [HttpServer getAdsWithType:@"index" WithSuccessBlock:^(NSDictionary *successDic) {
        NSMutableArray *imagesArray = [[NSMutableArray alloc]init];
        NSMutableArray *urlsArray = [[NSMutableArray alloc]init];
        for (id item in [successDic objectForKey:@"ads"]) {
            [imagesArray addObject:[item objectForKey:@"picture"]];
            [urlsArray addObject:[item objectForKey:@"jumpUrl"]];
        }
        [banner refreshBannerWithImageUrls:imagesArray RelatedUrls:urlsArray];
    } withFailBlock:^(id failData) {
        NSLog(@"%@",failData);
    }];
}

#pragma mark - Text field delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [textField resignFirstResponder];
    if (!searchVC) {
        searchVC = [[SearchViewController alloc]initWithNibName:@"SearchViewController" bundle:nil];
    }
    [self.navigationController pushViewController:searchVC animated:true];
}

#pragma mark - Table view MJRefresh related methods
- (void)loadMoreData {
    [params setObject:[NSString stringWithFormat:@"%d",page] forKey:@"page"];
    [self loadHousesData];
}

- (void)refreshData {
    page = 1;
    [params setObject:@"1" forKey:@"page"];
    [params setObject:@"10" forKey:@"pageSize"];
    [housesArray removeAllObjects];
    [mainTableView setContentOffset:CGPointZero animated:true];
    [self loadHousesData];
}

- (void)pullToRefreshData {
    page = 1;
    [params setObject:@"1" forKey:@"page"];
    [params setObject:@"10" forKey:@"pageSize"];
    [housesArray removeAllObjects];
    [self loadHousesData];
}

- (void)pageClickedWithDestinationUrl:(NSString *)url {
    WebViewController *webVC = [[WebViewController alloc]init];
    webVC.destinationUrl = url;
    webVC.titleString = @"房妈妈";
    [self.navigationController pushViewController:webVC animated:true];
}

#pragma mark - Filter views delegate
- (void)doneFilterWithDistrict:(NSString *)district WithRegion:(NSString *)region WithRegionCode:(NSString *)regionCode {
    [params setObject:regionCode forKey:@"regionCode"];
    [self refreshData];
    if ([region isEqual:@"全部"]) {
        filterRegionButtonString = [NSString stringWithFormat:@"%@",district];
    } else {
        if ([region isEqualToString:@""]) {
            filterRegionButtonString = [NSString stringWithFormat:@"%@", @"区域"];
        } else {
            filterRegionButtonString = [NSString stringWithFormat:@"%@", region];
        }
    }
}

- (void)didSelectedRow:(NSInteger)row WithItem:(id)item {
    switch (row) {
        case 0:
            [params setObject:@"" forKey:@"order"];
            filterSortByButtonString = @"排序";
            break;
        case 1:
            [params setObject:@"rent_asc" forKey:@"order"];
            filterSortByButtonString = @"价格排序";
            break;
        case 2:
            [params setObject:@"room_desc" forKey:@"order"];
            filterSortByButtonString = @"房间排序";
            break;
        case 3:
            [params setObject:@"leader_desc" forKey:@"order"];
            filterSortByButtonString = @"室长排序";
            break;
        default:
            break;
    }
    [self refreshData];
}

- (void)filterDoneWithHouseCount:(NSString *)count WithLowerPrice:(NSInteger)lowerPrice UpperPrice:(NSInteger)upperPrice {
    [params setObject:count forKey:@"roomNum"];
    NSString *rent = [NSString stringWithFormat:@"%ld-%ld",(long)lowerPrice, (long)upperPrice];
    [params setObject:rent forKey:@"rent"];
    if ([count isEqual:@""]) {
        if (lowerPrice == 0 && upperPrice == 5000) {
            filterPriceButtonString = [NSString stringWithFormat:@"价格"];
        } else {
            filterPriceButtonString = [NSString stringWithFormat:@"¥%ld-%ld", (long)lowerPrice, (long)upperPrice];
        }
    } else {
        if (lowerPrice == 0 && upperPrice == 5000) {
            filterPriceButtonString = [NSString stringWithFormat:@"价格"];
        } else {
            filterPriceButtonString = [NSString stringWithFormat:@"%@室¥%ld-%ld", count, (long)lowerPrice, (long)upperPrice];
        }
    }
    [self refreshData];
}

#pragma mark - Table view cell delegate
- (void)headerButtonClickedWithTag:(NSInteger)tag WithIndex:(NSInteger)index{
    NSArray *tempTeams = [housesArray[index] objectForKey:@"teams"];
    NSDictionary *leaderDic = tempTeams[tag];
    Leader *leader = [[Leader alloc]initWithDic:leaderDic];
    UserHomepageViewController *userHomepageVC = [[UserHomepageViewController alloc]init];
    userHomepageVC.userId = leader.leaderId;
    userHomepageVC.userName = leader.leaderName;
    [self.navigationController pushViewController:userHomepageVC animated:true];
}

- (void)clickToChooseCity {
    cityVC = [[ChooseCityViewController alloc]initWithNibName:@"ChooseCityViewController" bundle:nil];
    [self presentViewController:cityVC animated:true completion:^{
        
    }];
}

//End refreshing
- (void)refreshToRequirement {
    [mainTableView reloadData];
}

#pragma mark - Fill requirement delegate
- (void)clickToPresent:(UIViewController *)destinationController {
    [self presentViewController:destinationController animated:true completion:^{
        
    }];
}

- (void)likeButtonClicked:(UIButton *)sender withIndex:(NSInteger)index {
    NSInteger row = index;
    House *house = [[House alloc]initWithDic:[housesArray[row] objectForKey:@"house"]];
    [self requestToFavorHouseWithId:house.houseId];
}

- (void)requestToFavorHouseWithId:(NSString *)houseId {
    if (houseId == nil || [houseId isEqual:@""]) {
        NSLog(@"houseId is nil");
    } else {
        [HttpServer postToFavorHouseWithHouseId:houseId WithSuccessBlock:^(NSDictionary *favoritesData) {
//            isFavorite = true;
//            favoriteId = [[favoritesData objectForKey:@"favorite"] objectForKey:@"favoriteId"];
//            [banner.likeButton setBackgroundImage:[UIImage imageNamed:@"detail_like_click"] forState:UIControlStateNormal];
//            [TSMessage showNotificationInViewController:[AppDrawer sharedDrawer] title:@"收藏成功!" subtitle:@"您已成功收藏该房源" type:TSMessageNotificationTypeSuccess duration:2.0 canBeDismissedByUser:true];
        } withFailBlock:^(id failData) {
            NSLog(@"%@",failData);
        }];
    }
}

//- (void)requestToDeleteFavorite {
//    if (favoriteId == nil || [favoriteId isEqual:@""]) {
//        NSLog(@"favoriteId is nil");
//    } else {
//        [HttpServer deleteFavoriteHouseWithId:favoriteId WithSuccessBlock:^(NSDictionary *successDic) {
////            isFavorite = false;
////            favoriteId = @"";
////            [banner.likeButton setBackgroundImage:[UIImage imageNamed:@"detail_like_non"] forState:UIControlStateNormal];
//        } withFailBlock:^(id failData) {
//            NSLog(@"%@",failData);
//        }];
//    }
//}

@end
